﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ModulesInfrastructure.ViewModels;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.Events;
using System.ComponentModel;
using Microsoft.Practices.ServiceLocation;
using ServerConnection.ShopServiceReference;
using ServerConnection;
using ShellInfrastructure;
using ModuleOrderItem.View;
using EventInfrastructure;

namespace ModuleOrderItem.ViewModel
{
    /// <summary>
    /// ViewModel of the order for watching it's details
    /// </summary>
    public class OrderItemViewModel : SharedViewModel, IOrderItemViewModel
    {

        #region Constructor

        /// <summary>
        /// The constructor of the OrderItemViewModel
        /// </summary>
        /// <param name="eventAggregator"></param>
        public OrderItemViewModel(IEventAggregator eventAggregator)
        {
            this.m_eventAggregator = eventAggregator;
            eventAggregator.GetEvent<OrderSelectedEvent>().Subscribe(onDrawRegionEvent);
        }

        #endregion


        #region Public properties

        [Dependency]
        public IRegionManager RegionManager { get; set; }


        /// <summary>
        /// Current selected order
        /// </summary>
        public OrderFull ShowOrder { get; private set; }

        #endregion


        #region Private methods

        /// <summary>
        /// Shows full information of the order and loads it to the right region
        /// </summary>
        /// <param name="id"></param>
        private void onDrawRegionEvent(long id)
        {
            RegionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
            IRegion region = RegionManager.Regions[RegionNames.RightRegionName];
            region.Activate(ServiceLocator.Current.GetInstance<IOrderViewItem>());

            this.ShowOrder = new OrderFull();
            this.ShowOrder = Service.Instance.GetOrder(id, "eng");
            RaisePropertyChanged(() => this.ShowOrder);
        }

        #endregion


        #region Private members

        private IEventAggregator m_eventAggregator;

        #endregion
    }
}
